#include "StdAfx.h"
#include "GraphicShowVideo.h"
#include "MultiMediaDlg.h"

IMPLEMENT_DYNAMIC(GraphicShowVideo,CWnd)

GraphicShowVideo *g_Instance;

GraphicShowVideo* GraphicShowVideo::ShareInstance()
{
	return g_Instance;
}

GraphicShowVideo::GraphicShowVideo(void)
{
	g_Instance = this; 
	m_Sub = NULL;
}

GraphicShowVideo::~GraphicShowVideo(void)
{
}
BEGIN_MESSAGE_MAP(GraphicShowVideo, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_WINDOWPOSCHANGED()
END_MESSAGE_MAP()

int GraphicShowVideo::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	m_Sub = new TestSubTitle2(this);
	m_Sub->Create(TestSubTitle2::IDD, this);
	CRect rc; 
	this->GetClientRect(rc); 
	this->ScreenToClient(rc);
	//GetWindowRect(rc);

	m_Sub->SetWindowPos(&CWnd::wndTop, rc.left+225,rc.top  + 50 + rc.Height() * 0.85,rc.Width()-15,rc.Height()/4-50/*rc.left + 210, rc.top  + 50 + rc.Height() * 0.85, rc.Width(), rc.Height()/4*/, SWP_NOOWNERZORDER);
	m_Sub->ShowWindow(SW_SHOW);

	return 0;
}

void GraphicShowVideo::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
	CWnd *parent = GetParent();
	CRect rect;
	parent->GetClientRect(&rect);
	CRect rtViewArea(rect.left,rect.top,rect.right, rect.bottom);
	this->GetClientRect(&m_rtPlayer);
	//CMultiMediaDlg::ShareInstance()->UpdateVideoScreen();

	CRect rc; 
	this->GetClientRect(rc); 
	//GetWindowRect(rc);
	m_Sub->SetWindowPos(&CWnd::wndTop, rc.left+225,rc.top  + 50 + rc.Height() * 0.85,rc.Width()-15,rc.Height()/4-50/*rc.left + 210, rc.top  + 50 + rc.Height() * 0.85, rc.Width(), rc.Height()/4*/, SWP_NOOWNERZORDER);
	
}


void  GraphicShowVideo::SetWndParentForVideoPlayerManager()
{
	CMultiMediaDlg::ShareInstance()->m_VideoMng.SetWindowParent(this);
}


void GraphicShowVideo::OnWindowPosChanged(WINDOWPOS* lpwndpos)
{
	CWnd::OnWindowPosChanged(lpwndpos);
	CRect rc; 
	this->GetClientRect(rc); 
	//GetWindowRect(rc);
	m_Sub->SetWindowPos(&CWnd::wndTop, rc.left+225,rc.top  + 50 + rc.Height() * 0.85,rc.Width()-15,rc.Height()/4-50/*rc.left + 210, rc.top  + 50 + rc.Height() * 0.85, rc.Width(), rc.Height()/4*/, SWP_NOOWNERZORDER);
	// TODO: Add your message handler code here
}

BOOL GraphicShowVideo::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message== WM_GRAPHNOTIFY)
	{
		HandleEventFromGraph();
	}

	return CWnd::PreTranslateMessage(pMsg);
}
void GraphicShowVideo::HandleEventFromGraph()
{
	//MediaPlay();
	HRESULT hr = S_OK;
	long evCode, param1,param2;
	if(!/*m_VideoMng.pMediaEventEx*/CMultiMediaDlg::ShareInstance()->m_VideoMng.pMediaEventEx)
	{
		AfxMessageBox(_T("MediaEventEx interface error"));
	}
	while (SUCCEEDED(/*m_VideoMng.pMediaEventEx*/CMultiMediaDlg::ShareInstance()->m_VideoMng.pMediaEventEx->GetEvent(&evCode, &param1, &param2, 0)))
	{
		switch(evCode)
		{
		case EC_OLE_EVENT:
			{
				m_Sub->SetTextCaption((BSTR)param2, false);
			}
			break;
		case EC_COMPLETE:
			//SetPlayListPropertiesValueWhenComplete();	
			
			break;
		}
		hr = CMultiMediaDlg::ShareInstance()->m_VideoMng.pMediaEventEx->FreeEventParams(evCode, param1, param2);
	}
}
